package logic

import (
	"common/model"
	"context"
	"errors"
	product "product_server/proto"
)

func (p *ProductServer) SpikesProductInfo(ctx context.Context, req *product.SpikesProductInfoRequest) (*product.SpikesProductInfoResponse, error) {
	spikesProduct, err := model.GetSpikesProductInfo(req.Id)
	if err != nil {
		return nil, errors.New("秒杀商品不存在")
	}

	info, err := model.GetProductInfo(spikesProduct.ProductId)
	if err != nil {
		return nil, errors.New("商品不存在")
	}

	return &product.SpikesProductInfoResponse{
		Id:            spikesProduct.Id,
		ProductId:     spikesProduct.ProductId,
		StoreName:     info.StoreName,
		Image:         info.Image,
		Sales:         info.Sales,
		SeckillPrice:  float32(spikesProduct.SeckillPrice),
		OriginalPrice: float32(spikesProduct.OriginalPrice),
		StartTime:     spikesProduct.StartTime.Format("2006-01-02 15:04:05"),
		EndTime:       spikesProduct.EndTime.Format("2006-01-02 15:04:05"),
		SpikesStock:   spikesProduct.SeckillStock,
	}, nil
}
